const Mock = require("mockjs");
const Random = Mock.Random;

const { optionalFieldRes } = require('./optionalFieldRes')

const fieldTypes = ['string', 'number', 'datetime']

function updateOptionalField() {

  let i = 0;
  for (let field of optionalFieldRes.data) {
    let j = i % 3;
    field.fieldType = fieldTypes[j]

    i++
  }

  return optionalFieldRes
}

updateOptionalField()


const allOptionalFields = optionalFieldRes.data;

function createTableRow(id) {

  const rowData = { 'rowId': id }

  for (let field of allOptionalFields) {

    let { fieldEnName, fieldType } = field

    switch (fieldType) {
      case 'string':
        rowData[fieldEnName] = Random.guid()
        break;

      case 'number':
        rowData[fieldEnName] = Random.integer(1, 10000)
        break;

      case 'datetime':
        rowData[fieldEnName] = Random.date("yyyy-MM-dd")
        break
    }
  }

  return rowData;
}

function getTableData() {
  const tableData = []
  for (let i = 0; i < 30; i++) {
    const row = createTableRow(i)
    tableData.push(row)

  }

  const result = {
    "success": true,
    "statusCode": 200,
    "message": null,
    'data': tableData
  }

  return result
}


const queryResult = getTableData()


Mock.mock(
  RegExp("/flowAnalysis/getOptionalField/*"),
  "get",
  optionalFieldRes
);

Mock.mock(
  RegExp("/flowAnalysis/selectFlowList/*"),
  "post",
  queryResult
);


